package main;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import utils.SmallTool;
import utils.StringUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;

public class 统计期刊数量 {
    @SneakyThrows
    public static void main(String[] args) {


        String dbNameMy = "spider_pubmed_vip_extend";

        Set<String> allPmid = new ConcurrentHashSet<>();
        int breakSize = 100000;
        SmallTool.printMemory();
        Db use3307 = Db.use();
        String allDate = "select max(id) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));
        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        IntStream.rangeClosed(1, pageSize).parallel().forEach(aa -> {
            int start = breakSize * (aa - 1) + 1;
            int end = aa * breakSize;
            SmallTool.printMess("开始页码++" + aa + "++id++" + start + "结束id++++" + end);
            String meSql = "select id,med_nlm_id from " + dbNameMy + " where id between " + start + " and " + end;
            List<Entity> queryMe = null;
            try {
                queryMe = use3307.query(meSql);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            queryMe.parallelStream().forEach(pmDeatil -> {
                String nlmId = pmDeatil.getStr("med_nlm_id");
                if (StringUtil.isNotNull(nlmId)) {
                    allPmid.add(nlmId);

                }
            });
        });

        System.out.println("全部期刊数量+" + allPmid.size());
        FileUtil.writeUtf8String(JSONObject.toJSONString(allPmid), "/usr/local/gcr/20240930/nlmid.log");

    }
}
